package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipReqularCheck;
import cc.comac.mom.bean.EquipReqularCheckBean;
import cc.comac.mom.bean.EquipReqularCheckItem;
import cc.comac.mom.bean.EquipReqularCheckSelectBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipReqularCheckDao;
import cc.comac.mom.dao.entity.EquipReqularCheckItemDao;
import cc.comac.mom.entity.EquipDevCategoryEntity;
import cc.comac.mom.entity.EquipReqularCheckEntity;
import cc.comac.mom.entity.EquipReqularCheckItemEntity;
import cc.comac.mom.redis.IdFactory;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.MomConstants;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipReqularCheckDataHandler  extends ServiceImpl<EquipReqularCheckDao, EquipReqularCheckEntity>{

	@Autowired
	private EquipReqularCheckDao equipReqularCheckDao;
		
	@Autowired
	private EquipReqularCheckItemDao equipReqularCheckItemDao;
	@Autowired
	private EquipDevCategoryDataHandler equipDevCategoryDataHandler;
	
	@Autowired
	private IdFactory idFactory;
	@Autowired
	private BaseEntityDataUtil 	baseEntityDataUtil;
	
	public ResultBean save(EquipReqularCheck obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipReqularCheckEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevCategoryEntity devTypeEntity = equipDevCategoryDataHandler.getById(innerData.getDevCategoryId());
		if(devTypeEntity == null) {
			throw new RuntimeException("设备类型信息不存在");
		}
		innerData.setBenchmarkCode(idFactory.generateCheckBaseNo(devTypeEntity.getDevCategoryNumber()));
		innerData.setBenchmarkStatus(MomConstants.EQUIP_BENCHMARK_STATUS_ENABLE);
		baseEntityDataUtil.setBaseEntityData(innerData);

		//判断是否存在重复
		QueryWrapper<EquipReqularCheckEntity> queryWrapper = new QueryWrapper<EquipReqularCheckEntity>();
		queryWrapper.eq("dev_category_id", obj.getInnerData().getDevCategoryId());
		queryWrapper.eq("benchmark_cycle",obj.getInnerData().getBenchmarkCycle());
		queryWrapper.eq("frequent_value", obj.getInnerData().getFrequentValue());
		queryWrapper.eq("frequent_type", obj.getInnerData().getFrequentType());
		queryWrapper.eq("version", obj.getInnerData().getVersion());
		List<EquipReqularCheckEntity> selectList = equipReqularCheckDao.selectList(queryWrapper);
		if (selectList!=null&&selectList.size() > 0) {

			throw new RuntimeException("点检标准的设备类型、点检责任、频率、版本号重复");
		}

		int insert = equipReqularCheckDao.insert(innerData);
		if(insert<1) {
			throw new RuntimeException("新增失败");
		}
		resultBean.setMessage(String.valueOf(innerData.getId()));
		return resultBean;
	}
	
	public ResultBean update(EquipReqularCheck obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipReqularCheckEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		baseEntityDataUtil.updateBaseEntityData(innerData);
		int update = equipReqularCheckDao.updateById(innerData);
		if(update<1) {
			throw new RuntimeException("修改失败");
		}
		return resultBean;
	}
	
	public ResultBean disableEquipReqularCheckByIds(List<Long> list) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub		
		List<EquipReqularCheckEntity> selectBatchIds = equipReqularCheckDao.selectBatchIds(list);
		if(selectBatchIds == null || selectBatchIds.size()<1) {
			throw new RuntimeException("数据为空");
		}
		for(EquipReqularCheckEntity item:selectBatchIds) {
			if(item.getBenchmarkStatus() != null && MomConstants.EQUIP_BENCHMARK_STATUS_DISABLED.equals(item.getBenchmarkStatus())) {
				item.setBenchmarkStatus(MomConstants.EQUIP_BENCHMARK_STATUS_ENABLE);
			}else {
				item.setBenchmarkStatus(MomConstants.EQUIP_BENCHMARK_STATUS_DISABLED);
			}
			
			baseEntityDataUtil.updateBaseEntityData(item);
		}
		boolean batchById = updateBatchById(selectBatchIds);
		if(!batchById) {
			throw new RuntimeException("修改失败");
		}
		return resultBean;
	}
	
	public ResultBean delete(EquipReqularCheck obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipReqularCheckEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		EquipReqularCheckEntity selectById = equipReqularCheckDao.selectById(innerData.getId());
		if(selectById == null) {
			throw new RuntimeException("删除数据不存在");
		}
		baseEntityDataUtil.deleteBaseEntityData(selectById);
		int delete = equipReqularCheckDao.updateById(selectById);
		if(delete<1) {
			throw new RuntimeException("删除失败");
		}
		return resultBean;
	}
	
	public ResultBean deleteBatch(List<EquipReqularCheck> list) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");	
		}
		ArrayList<Long> listIds = new ArrayList<Long>();
		for(EquipReqularCheck item:list) {
			EquipReqularCheckEntity innerData = item.getInnerData();
			if(innerData == null) {
				throw new RuntimeException("数据为空");	
			}
			listIds.add(innerData.getId());
		}
		List<EquipReqularCheckEntity> selectBatchIdsList = equipReqularCheckDao.selectBatchIds(listIds);
		if(selectBatchIdsList == null || selectBatchIdsList.size()<1) {
			throw new RuntimeException("数据为空");
		}
	
		for(EquipReqularCheckEntity item :selectBatchIdsList) {
			baseEntityDataUtil.deleteBaseEntityData(item);
		}
		boolean updateBatchById = updateBatchById(selectBatchIdsList);
		if(!updateBatchById) {
			throw new RuntimeException("删除失败");
		}

		return resultBean;
	}
	
	public ResultDev selectByFields(EquipReqularCheckSelectBean obj) {
		ResultDev resultDev = new ResultDev();
		if (obj.getPageSize() == 0L) {
			obj.setPageSize(10);
		}
		obj.setPageNum((obj.getPageNum()-1)*obj.getPageSize());
		List<EquipReqularCheckSelectBean> selectByFields = equipReqularCheckDao.selectByFields(obj);
		QueryWrapper<EquipReqularCheckEntity> queryWrapper = new QueryWrapper<EquipReqularCheckEntity>();
		queryWrapper.eq("row_status", 1);
		if(obj.getDevCategoryId() != 0) {
			queryWrapper.eq("dev_category_id",obj.getDevCategoryId());
		}
		if(obj.getBenchmarkStatus()!= null && !"".equals(obj.getBenchmarkStatus())) {
			queryWrapper.eq("benchmark_status",obj.getBenchmarkStatus());
		}
		if(obj.getBenchmarkCycle()!= null && !"".equals(obj.getBenchmarkCycle())) {
			queryWrapper.like("benchmark_cycle",obj.getBenchmarkCycle());
		}
		if(obj.getFrequentType()!= null && !"".equals(obj.getFrequentType())) {
			queryWrapper.like("frequent_type",obj.getFrequentType());
		}
		if(obj.getFrequentValue() != 0) {
			queryWrapper.like("frequent_value",obj.getFrequentValue());
		}
		queryWrapper.orderByDesc("creation_time");
		Integer selectCount = equipReqularCheckDao.selectCount(queryWrapper);
		// 将查询总数除以每页数量后向上取整获取总页数
		double pageCount = Math.ceil((double)selectCount / obj.getPageSize());
		resultDev.setEntityList(selectByFields);
		resultDev.setPageCount((int)pageCount);
		resultDev.setTotalQuantity((int)selectCount);
		return resultDev;
		
	}
	
	public EquipReqularCheckBean selectByCondition(EquipReqularCheck obj) {
    	QueryWrapper<EquipReqularCheckEntity> queryWrapper = new QueryWrapper<EquipReqularCheckEntity>();
    	queryWrapper.eq("row_status",1);
    	if(obj != null && obj.getInnerData() != null) {
    		
    		if(obj.getInnerData().getDevCategoryId() != 0) {
    			queryWrapper.eq("dev_category_id",obj.getInnerData().getDevCategoryId());
    		}
    		if(obj.getInnerData().getBenchmarkCycle()!= null && !"".equals(obj.getInnerData().getBenchmarkCycle())) {
    			queryWrapper.eq("benchmark_cycle",obj.getInnerData().getBenchmarkCycle());
    		}
    		if(obj.getInnerData().getFrequentType()!= null && !"".equals(obj.getInnerData().getFrequentType())) {
    			queryWrapper.eq("frequent_type",obj.getInnerData().getFrequentType());
    		}
    		if(obj.getInnerData().getFrequentValue() != 0) {
    			queryWrapper.eq("frequent_value",obj.getInnerData().getFrequentValue());
    		}
    	}
        Page<EquipReqularCheckEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
        IPage<EquipReqularCheckEntity> iPage = equipReqularCheckDao.selectPage(page, queryWrapper);
        List<EquipReqularCheckEntity> list = iPage.getRecords();
        if(list != null && list.size()>0) {
        	long pages = iPage.getPages();
        	long total = iPage.getTotal();
        	ArrayList<EquipReqularCheck> arrayList = new ArrayList<EquipReqularCheck>();
        	for(EquipReqularCheckEntity item :list) {
        		EquipReqularCheck EquipReqularCheck = new EquipReqularCheck();
        		EquipReqularCheck.setInnerData(item);
        	arrayList.add(EquipReqularCheck);
        	}
        	EquipReqularCheckBean EquipReqularCheck = new EquipReqularCheckBean();
        	EquipReqularCheck.setList(arrayList);
        	EquipReqularCheck.setPages((int)pages);
        	EquipReqularCheck.setTotal((int)total);
        	return EquipReqularCheck;
        }
		return null;
		
	}
	
	public EquipReqularCheck getById(long id,long pageNum,long pageSize) {
		// TODO Auto-generated method stub
		EquipReqularCheckEntity appendEntity = equipReqularCheckDao.selectById(id);
		if(appendEntity != null) {
			EquipReqularCheck equipReqularCheck = new EquipReqularCheck();
			equipReqularCheck.setInnerData(appendEntity);
			
	    	QueryWrapper<EquipReqularCheckItemEntity> queryWrapper = new QueryWrapper<EquipReqularCheckItemEntity>();
	    	queryWrapper.eq("row_status",1);
	    	queryWrapper.eq("regular_check_id",id);
	    	queryWrapper.orderByAsc("benchmark_seq");
	    	
	    	Page<EquipReqularCheckItemEntity> page = new Page<>(pageNum, pageSize, true);
		    IPage<EquipReqularCheckItemEntity> iPage = equipReqularCheckItemDao.selectPage(page, queryWrapper);
		    List<EquipReqularCheckItemEntity> selectList = iPage.getRecords();
			if(selectList == null || selectList.size()<1) {
				equipReqularCheck.setPages(0);
				equipReqularCheck.setTotal(0);
				return equipReqularCheck;
			}
			ArrayList<EquipReqularCheckItem> arrayList = new ArrayList<EquipReqularCheckItem>();
			for(EquipReqularCheckItemEntity item:selectList) {
				EquipReqularCheckItem equipReqularCheckItem = new EquipReqularCheckItem();
				equipReqularCheckItem.setInnerData(item);
				arrayList.add(equipReqularCheckItem);
			}
			equipReqularCheck.setListItem(arrayList);

			equipReqularCheck.setPages((int)iPage.getPages());
			equipReqularCheck.setTotal((int)iPage.getTotal());
			return equipReqularCheck;
		}
		return null;
	}
	
	//通过位置获取定检信息
	public ResultDev selectByWorkShop(EquipReqularCheckSelectBean obj) {
		ResultDev resultDev = new ResultDev();
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		obj.setPageNum((obj.getPageNum()-1)*obj.getPageSize());
		List<EquipReqularCheckSelectBean> listEquipReqularCheckSelectBean = equipReqularCheckDao.selectByWorkShop(obj);
		int countByFields = equipReqularCheckDao.selectCountByWorkShop(obj);
		
		// 将查询总数除以每页数量后向上取整获取总页数
		double pageCount = Math.ceil((double)countByFields / obj.getPageSize());
		resultDev.setEntityList(listEquipReqularCheckSelectBean);
		resultDev.setPageCount((int)pageCount);
		resultDev.setTotalQuantity(countByFields);
		return resultDev;
	}
}
